package cn.bellychang.ood.mailsendproduct.good;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author ChangLiang
 * @date 2020/5/12
 */
public class ProductService {

    /**
     * 读取文件 获取促销产品列表
     * @return
     */
    public List<Product> getPromotionProduct(){

        List<Product> list = new ArrayList<>();

        File file = new File(this.getClass().getClassLoader().getResource("product_promotion.txt").getFile());
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            while (Objects.nonNull(line = br.readLine())) {
                String[] data = line.split(" ");
                Product product = new Product();
                product.setId(data[0]);
                product.setDesc(data[1]);
                list.add(product);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}
